using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BusinessLayer;
using EntityLayer;
using WebControls;

public partial class Admin_Articles : SFPage
{
    busArticles insBusArticles = new busArticles();
    entArticles insEntArticles = new entArticles();
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected bool sfgArticles_NeedsDataSource()
    {
        DataTable insDataTableArticles = insBusArticles.SelectArticles();
        if (insBusArticles.HasErrors)
        {
            MessageBoxShow(insBusArticles);
        }
        sfgArticles.SetData(insDataTableArticles);
        return insBusArticles.HasErrors;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.Id = 0;
        this.OpenEditPanel();

    }
    protected void sfgArticles_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditData")
        {
            this.Id = Convert.ToInt32(e.CommandArgument);
            this.OpenEditPanel();
        }
        else if (e.CommandName == "DeleteData")
        {
            insEntArticles.ArticleId = Convert.ToInt32(e.CommandArgument);
            insBusArticles.DeleteArticlesByArticleId(insEntArticles);
            if (insBusArticles.HasErrors)
            {
                MessageBoxShow(insBusArticles);
                return;
            }
            sfgArticles.LoadGridData();
            this.CloseEditPanel();
        }
    }
    public override void OpenEditPanel()
    {
        this.Clear(this.Master.FindControl("ContentPlaceHolderEdit"));
        base.OpenEditPanel();
        lstArticles.LoadDataListData();

    }
    protected bool lstArticles_NeedsDataSource()
    {
        busArticleCategories insBusArticleCategories = new busArticleCategories();
        DataTable insDataTableArticleCategories = insBusArticleCategories.SelectArticleCategoriesByLanguageId();
        if (insBusArticleCategories.HasErrors)
        {
            MessageBoxShow(insBusArticleCategories);
            return true;
        }
        sfcArticleCategories.SetData(insDataTableArticleCategories, "ArticleCategoryName", "ArticleCategoryId");

        entArticleArticleCategories insEntArticleArticleCategories = new entArticleArticleCategories();
        insEntArticleArticleCategories.ArticleId = this.Id;
        busArticleArticleCategories insBusArticleArticleCategories = new busArticleArticleCategories();
        DataTable insDataTableArticleArticleCategories = insBusArticleArticleCategories.SelectArticleArticleCategoriesByArticleId(insEntArticleArticleCategories);
        if (insBusArticleArticleCategories.HasErrors)
        {
            MessageBoxShow(insBusArticleArticleCategories);
            return true;
        }
        sfcArticleCategories.SetItemsChecked(insDataTableArticleArticleCategories);

        insEntArticles.ArticleId = this.Id;
        insBusArticles.SelectArticlesByArticleId(insEntArticles);
        if (insBusArticles.HasErrors)
        {
            MessageBoxShow(insBusArticles);
            return true;
        }
        txtArticleDescription.Text = insEntArticles.ArticleDescription;
        calPublishDate.Value = insEntArticles.PublishDate;

        entArticleContents insEntArticleContents = new entArticleContents();
        insEntArticleContents.ArticleId = insEntArticles.ArticleId;
        busArticleContents insBusArticleContents = new busArticleContents();
        DataTable insDataTableArticleContents = insBusArticleContents.SelectArticleContentsByArticleId(insEntArticleContents);
        if (insBusArticleContents.HasErrors)
        {
            MessageBoxShow(insBusArticleContents);
            return true;
        }
        lstArticles.SetData(insDataTableArticleContents);
        return false;
    }

    public override void Save()
    {

        this.Validate();
        if (this.HasErrors)
        {
            MessageBoxShow(this);
            return;
        }

        insEntArticles.ArticleDescription = txtArticleDescription.Text;
        insEntArticles.PublishDate = calPublishDate.Value.Value;

        foreach (ListItem li in sfcArticleCategories.Items)
        {
            if (li.Selected)
            {
                entArticleArticleCategories insEntArticleArticleCategories = new entArticleArticleCategories();
                insEntArticleArticleCategories.ArticleCategoryId = Convert.ToInt32(li.Value);
                insEntArticles.ArticleArticleCategories.Add(insEntArticleArticleCategories);
            }
        }

        foreach (DataListItem dli in lstArticles.Items)
        {
            entArticleContents insEntArticleContents = new entArticleContents();
            if (((CheckBox)dli.FindControl("chkLanguage")).Checked)
            {
                insEntArticleContents.LanguageId = Convert.ToInt32(((SFCheckBox)dli.FindControl("chkLanguage")).Tag);
                insEntArticleContents.ArticleBody = ((SFEditor)dli.FindControl("ediArticleBody")).HTML;
                insEntArticleContents.ArticleSummary = ((SFEditor)dli.FindControl("ediArticleSummary")).HTML;
                insEntArticleContents.ArticleBodyText = ((SFEditor)dli.FindControl("ediArticleBody")).Text;
                insEntArticleContents.ArticleSummaryText = ((SFEditor)dli.FindControl("ediArticleSummary")).Text;
                insEntArticleContents.ArticleTitle = ((SFTextBox)dli.FindControl("txtArticleTitle")).Text;
                if (lstArticles.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntArticleContents.ArticleContentId = Convert.ToInt32(lstArticles.DataKeys[dli.ItemIndex]);
                    insEntArticles.ArticleContentsUpdate.Add(insEntArticleContents);
                }
                else
                {
                    insEntArticles.ArticleContentsInsert.Add(insEntArticleContents);
                }
                string[] locations = ((SFTextBox)dli.FindControl("txtArticleContentPublishLocations")).Text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                foreach (string s in locations)
                {
                    if (s.IndexOf("(") > -1 && s.IndexOf(")") > -1)
                    {
                        if (WebControls.Validation.ValidateWebSite(s.Substring(s.IndexOf("(") + 1, s.IndexOf(")") - s.IndexOf("(") - 1).Trim()))
                        {
                            entArticleContentPublishLocations insEntArticleContentPublishLocations = new entArticleContentPublishLocations();
                            insEntArticleContentPublishLocations.ArticleContentPublishLocationLink = s.Substring(s.IndexOf("(") + 1, s.IndexOf(")") - s.IndexOf("(") - 1).Trim();
                            insEntArticleContentPublishLocations.ArticleContentPublishLocationName = s.Substring(0, s.IndexOf("(")).Trim();
                            insEntArticleContents.ArticleContentPublishLocations.Add(insEntArticleContentPublishLocations);
                        }
                        else
                        {
                            MessageBoxShow(StaticMethods.GetErrorMessageMultilanguageString(18, new object[] { ((SFLabel)dli.FindControl("lblArticleContentPublishLocations")).Text }));
                            return;
                        }
                    }
                    else
                    {
                        MessageBoxShow(StaticMethods.GetErrorMessageMultilanguageString(18, new object[] { ((SFLabel)dli.FindControl("lblArticleContentPublishLocations")).Text }));
                        return;
                    }
                }
            }
            else
            {
                if (lstArticles.DataKeys[dli.ItemIndex] != DBNull.Value)
                {
                    insEntArticleContents.ArticleContentId = Convert.ToInt32(lstArticles.DataKeys[dli.ItemIndex]);
                    insEntArticles.ArticleContentsDelete.Add(insEntArticleContents);
                }
            }
        }
        base.Save();
    }
    public override void SaveNew()
    {
        insBusArticles.InsertArticles(insEntArticles);
        if (insBusArticles.HasErrors)
        {
            MessageBoxShow(insBusArticles);
            return;
        }
        base.SaveNew();
        sfgArticles.LoadGridData();
    }
    public override void SaveExisting()
    {
        insEntArticles.ArticleId = this.Id;
        insBusArticles.UpdateArticlesByArticleId(insEntArticles);
        if (insBusArticles.HasErrors)
        {
            MessageBoxShow(insBusArticles);
            return;
        }
        base.SaveExisting();
        sfgArticles.LoadGridData();
    }

    protected void lstArticles_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item != null && e.Item.FindControl("chkLanguage") != null && e.Item.DataItem != null)
        {
            if (this.Id != 0)
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = (((DataRowView)e.Item.DataItem)["ArticleContentId"] != DBNull.Value);
                e.Item.FindControl("pnlArticleContents").Visible = ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked;
            }
            else
            {
                ((SFCheckBox)e.Item.FindControl("chkLanguage")).Checked = true;
            }
            ((SFCheckBox)e.Item.FindControl("chkLanguage")).Tag = ((DataRowView)e.Item.DataItem)["LanguageId"].ToString();
        }
    }
    protected void Check(object sender, System.EventArgs e)
    {
        this.OpenEditPanelBase();
        ((SFCheckBox)sender).NamingContainer.FindControl("pnlArticleContents").Visible = ((SFCheckBox)sender).Checked;
    }
}
